package com.xrh.core.generater.mybatisplus.generater;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xrh.core.generater.bean.DataBaseConfig;
import com.xrh.core.generater.bean.UserInput;
import com.xrh.core.generater.interfaces.Console;
import com.xrh.core.generater.utils.FileUtils;
import com.xrh.core.generater.utils.OSUtils;
import com.xrh.core.generater.utils.PathUtils;



public class MybatisPlusGeneraterUI  extends JFrame implements  Console  {
	
	//缓存文件名称
	public final static String CACHE_FILE_NAME = "mybatisPlusUserInput.cache";
	
	//缓存的路径
	public final static String CACHE_FILE_PATH = PathUtils.getUserHomePath()  + File.separator + "JavaApp"+File.separator+"CodeGenerater" + File.separator + CACHE_FILE_NAME ;
	
	private static final long serialVersionUID = 8500817386469482382L;
	private JTextField txtComewellkmservicecontrollers;
	private JTextField textField_7;
	private JTextField textField_8;
	private JTextField txtComewellkmserviceservice;
	private JTextField txtComewellkmapientity;
	private JTextField txtComewellkmapidto;
	private JTextField txtComewellkmapirequestupdaterequest;
	private JTextField txtComewellkmapirequestaddrequest;
	private JTextField txtComewellkmapirequestqueryrequest;
	private JTextField txtComewellkmservicemapper;
	private JTextField textField;
	private JTextField txtRoot;
	private JTextField textField_2;
	private JTextField txtKm;
	private JTextField txtRoot_1;
	private JTextField textField_15;
	private JTextArea textArea;
	private JButton btnNewButton;
	private JTextField authorInput;
	
	public MybatisPlusGeneraterUI() {
		
		//关闭窗口
		this.addWindowListener(new WindowAdapter() {
	            public void windowClosing(WindowEvent e) {
	                System.exit(0);
	        	};
		});
		
		MybatisPlusGeneraterUI mybatisPlusGeneraterUI = this;
		
		setBounds(100, 100, 721, 597);
		setResizable(false);
		setTitle("Mybatis-Plus代码生成器");
		
		getContentPane().setLayout(new CardLayout(0, 0));
		
		JPanel panel = new JPanel();
		getContentPane().add(panel, "name_90880623278465");
		
		JLabel lblNewLabel_2_3 = new JLabel("controller包路径 :");
		
		txtComewellkmservicecontrollers = new JTextField();
		txtComewellkmservicecontrollers.setText("com.sy.km.service.controllers");
		txtComewellkmservicecontrollers.setColumns(10);
		
		JLabel lblNewLabel_2_3_2 = new JLabel("updateRequest包路径 :");
		
		textField_7 = new JTextField();
		textField_7.setColumns(10);
		
		JLabel lblNewLabel_2_3_1_1 = new JLabel("updateRequest包路径 :");
		
		textField_8 = new JTextField();
		textField_8.setColumns(10);
		
		JLabel lblNewLabel_2_3_1 = new JLabel("service包路径 :");
		
		txtComewellkmserviceservice = new JTextField();
		txtComewellkmserviceservice.setText("com.sy.km.service.service");
		txtComewellkmserviceservice.setColumns(10);
		
		JLabel lblNewLabel_2_3_3 = new JLabel("enttity包路径 :");
		
		txtComewellkmapientity = new JTextField();
		txtComewellkmapientity.setText("com.sy.km.api.entity");
		txtComewellkmapientity.setColumns(10);
		
		JLabel lblNewLabel_2_3_1_2 = new JLabel("dto包路径 :");
		
		txtComewellkmapidto = new JTextField();
		txtComewellkmapidto.setText("com.sy.km.api.dto");
		txtComewellkmapidto.setColumns(10);
		
		JLabel lblNewLabel_2_3_4 = new JLabel("mapper包路径 :");
		
		JLabel lblNewLabel_2_3_3_1 = new JLabel("updateRequest包路径 :");
		
		txtComewellkmapirequestupdaterequest = new JTextField();
		txtComewellkmapirequestupdaterequest.setText("com.sy.km.api.request.updateRequest");
		txtComewellkmapirequestupdaterequest.setColumns(10);
		
		JLabel lblNewLabel_2_3_1_2_1 = new JLabel("queryRequest包路径 :");
		
		JLabel lblNewLabel_2_3_1_3 = new JLabel("addRequest包路径 :");
		
		txtComewellkmapirequestaddrequest = new JTextField();
		txtComewellkmapirequestaddrequest.setText("com.sy.km.api.request.addRequest");
		txtComewellkmapirequestaddrequest.setColumns(10);
		
		txtComewellkmapirequestqueryrequest = new JTextField();
		txtComewellkmapirequestqueryrequest.setText("com.sy.km.api.request.queryRequest");
		txtComewellkmapirequestqueryrequest.setColumns(10);
		
		txtComewellkmservicemapper = new JTextField();
		txtComewellkmservicemapper.setText("com.sy.km.service.mapper");
		txtComewellkmservicemapper.setColumns(10);
		
		JLabel lblNewLabel_2_3_5 = new JLabel("数据库IP :");
		
		textField = new JTextField();
		textField.setText("127.0.0.1");
		textField.setColumns(10);
		
		JLabel lblNewLabel_2_3_1_4 = new JLabel("端口号 :");
		
		JLabel lblNewLabel_2_3_1_2_2 = new JLabel("数据库账号 :");
		
		txtRoot = new JTextField();
		txtRoot.setText("root");
		txtRoot.setColumns(10);
		
		textField_2 = new JTextField();
		textField_2.setText("3306");
		textField_2.setColumns(10);
		
		txtKm = new JTextField();
		txtKm.setText("km");
		txtKm.setColumns(10);
		
		JLabel lblNewLabel_2_3_3_2 = new JLabel("数据库名称 :");
		
		txtRoot_1 = new JTextField();
		txtRoot_1.setText("root");
		txtRoot_1.setColumns(10);
		
		JLabel lblNewLabel_2_3_4_1 = new JLabel("数据库密码 :");
		
		JSeparator separator = new JSeparator();
		
		JSeparator separator_1 = new JSeparator();
		
		JLabel lblNewLabel_2_3_3_1_1 = new JLabel("控制台输出 :");
		
		btnNewButton = new JButton("生成代码");
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				btnNewButton.setEnabled(false);
				//点击代码生成按钮 , 触发生成代码
				new  MybatisPlusGenerater().generateFiles(mybatisPlusGeneraterUI, getUserInput());
			}
		});
		
		JLabel lblNewLabel_2_3_4_1_1 = new JLabel("输出目录 :");
		
		textField_15 = new JTextField();
		textField_15.setForeground(Color.BLUE);
		textField_15.setBackground(Color.LIGHT_GRAY);
		textField_15.setEditable(false);
		textField_15.setColumns(10);
		textField_15.setText(PathUtils.getDesktopPath() + File.separator + "Mybatis-Plus代码生成器");
		
		JButton btnNewButton_1 = new JButton("打开");
		btnNewButton_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				
				JFileChooser jfc = new JFileChooser(); 
		        jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY );  //只能打开目录
		        jfc.setDialogTitle("选择输出目录");
		        jfc.showDialog(new JLabel(), "确定");  
		        File file = jfc.getSelectedFile();  
		        if(file != null && file.exists()){
		        	textField_15.setText(file.getAbsolutePath() + File.separator + "Mybatis-Plus代码生成器");
		        }
				
			}
		});
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		
		JButton btnNewButton_2 = new JButton("打开输出目录");
		btnNewButton_2.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				
				UserInput userInput = getUserInput();
				
				String[] cmd = new String[]{"cmd.exe","/c" , "start " + userInput.getOutputDirPath()};
				
				//不是windows这样打开
				if(!OSUtils.isWindows()) 
					cmd = new String[]{"bash","-c" , "open " + userInput.getOutputDirPath()};
				
				try {
					Runtime.getRuntime().exec(cmd);
				} catch (IOException e1) {
					console(e1.getMessage());
				}
				
			}
		});
		
		JLabel lblNewLabel = new JLabel("作者 :");
		
		authorInput = new JTextField();
		authorInput.setText("SY-CodeGenerater");
		authorInput.setColumns(10);
		
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addContainerGap()
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_panel.createSequentialGroup()
									.addGap(74)
									.addComponent(lblNewLabel_2_3_5)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(textField, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE)
									.addGap(91)
									.addComponent(lblNewLabel_2_3_1_4)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE))
								.addComponent(separator_1, GroupLayout.PREFERRED_SIZE, 1, GroupLayout.PREFERRED_SIZE)
								.addGroup(gl_panel.createSequentialGroup()
									.addGap(52)
									.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
										.addComponent(lblNewLabel_2_3_3_2)
										.addComponent(lblNewLabel_2_3_4_1, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE))
									.addPreferredGap(ComponentPlacement.RELATED)
									.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
										.addComponent(txtKm, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE)
										.addComponent(txtRoot_1, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE))
									.addGap(67)
									.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
										.addGroup(gl_panel.createSequentialGroup()
											.addComponent(lblNewLabel_2_3_1_2_2)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(txtRoot, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE))
										.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
											.addGroup(gl_panel.createSequentialGroup()
												.addComponent(btnNewButton)
												.addPreferredGap(ComponentPlacement.RELATED)
												.addComponent(btnNewButton_2))
											.addGroup(gl_panel.createSequentialGroup()
												.addComponent(lblNewLabel_2_3_4_1_1)
												.addPreferredGap(ComponentPlacement.RELATED)
												.addComponent(textField_15, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE)
												.addPreferredGap(ComponentPlacement.RELATED)
												.addComponent(btnNewButton_1)))))))
						.addGroup(gl_panel.createSequentialGroup()
							.addContainerGap()
							.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING, false)
								.addGroup(gl_panel.createSequentialGroup()
									.addComponent(lblNewLabel_2_3_4)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(txtComewellkmservicemapper, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE)
									.addGap(23)
									.addComponent(lblNewLabel_2_3_1_3)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(txtComewellkmapirequestaddrequest, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_panel.createSequentialGroup()
									.addGap(54)
									.addComponent(lblNewLabel_2_3_3)
									.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
									.addComponent(txtComewellkmapientity, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE)
									.addGap(74)
									.addComponent(lblNewLabel_2_3_1_2)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(txtComewellkmapidto, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_panel.createSequentialGroup()
									.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
										.addGroup(gl_panel.createSequentialGroup()
											.addComponent(lblNewLabel_2_3)
											.addPreferredGap(ComponentPlacement.RELATED))
										.addGroup(gl_panel.createSequentialGroup()
											.addComponent(separator, GroupLayout.PREFERRED_SIZE, 1, GroupLayout.PREFERRED_SIZE)
											.addGap(20)))
									.addComponent(txtComewellkmservicecontrollers, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE)
									.addGap(52)
									.addComponent(lblNewLabel_2_3_1)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(txtComewellkmserviceservice, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_panel.createSequentialGroup()
									.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
										.addGroup(gl_panel.createSequentialGroup()
											.addComponent(lblNewLabel)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(authorInput, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE))
										.addGroup(gl_panel.createSequentialGroup()
											.addComponent(lblNewLabel_2_3_3_1, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE)
											.addGap(6)
											.addComponent(txtComewellkmapirequestupdaterequest, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE)))
									.addGap(14)
									.addComponent(lblNewLabel_2_3_1_2_1)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(txtComewellkmapirequestqueryrequest, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE))))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(15)
							.addComponent(lblNewLabel_2_3_2, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE)
							.addGap(6)
							.addComponent(textField_7, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE)
							.addGap(12)
							.addComponent(lblNewLabel_2_3_1_1, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE)
							.addGap(6)
							.addComponent(textField_8, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(65)
							.addComponent(lblNewLabel_2_3_3_1_1)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 500, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(21, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(26)
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(32)
							.addComponent(lblNewLabel_2_3_5))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(32)
							.addComponent(lblNewLabel_2_3_1_4)))
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(6)
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
									.addComponent(txtKm, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
									.addComponent(lblNewLabel_2_3_3_2))
								.addComponent(txtRoot, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(12)
							.addComponent(lblNewLabel_2_3_1_2_2)))
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(36)
							.addComponent(separator_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(12)
							.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
								.addComponent(txtRoot_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblNewLabel_2_3_4_1_1)
								.addComponent(textField_15, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(btnNewButton_1)
								.addComponent(lblNewLabel_2_3_4_1))))
					.addGap(24)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(lblNewLabel_2_3)
							.addPreferredGap(ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
							.addComponent(separator, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addComponent(txtComewellkmservicecontrollers, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_2_3_1)
						.addComponent(txtComewellkmserviceservice, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addComponent(txtComewellkmapientity, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(txtComewellkmapidto, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(12)
							.addComponent(lblNewLabel_2_3_3))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(12)
							.addComponent(lblNewLabel_2_3_1_2)))
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addComponent(txtComewellkmservicemapper, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(txtComewellkmapirequestaddrequest, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addGroup(gl_panel.createSequentialGroup()
									.addGap(6)
									.addComponent(lblNewLabel_2_3_1_3))))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(12)
							.addComponent(lblNewLabel_2_3_4)))
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(6)
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_panel.createSequentialGroup()
									.addGap(6)
									.addComponent(lblNewLabel_2_3_3_1))
								.addComponent(txtComewellkmapirequestupdaterequest, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(txtComewellkmapirequestqueryrequest, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(12)
							.addComponent(lblNewLabel_2_3_1_2_1)))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel)
						.addComponent(authorInput, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(12)
							.addComponent(lblNewLabel_2_3_3_1_1))
						.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 172, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton_2)
						.addComponent(btnNewButton))
					.addGap(180)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(79)
							.addComponent(lblNewLabel_2_3_2))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(73)
							.addComponent(textField_7, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(79)
							.addComponent(lblNewLabel_2_3_1_1))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(73)
							.addComponent(textField_8, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
		);
		
		textArea = new JTextArea();
		textArea.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		textArea.setForeground(Color.BLUE);
		scrollPane.setViewportView(textArea);
		textArea.setWrapStyleWord(true);
		textArea.setLineWrap(true);
		textArea.setBackground(Color.LIGHT_GRAY);
		textArea.setEditable(false);
		panel.setLayout(gl_panel);
		
		
		//从缓存读取配置并初始化
		readCacheAndInitForm();
	}


	public static void main(String[] args) {
		
		try {
			UIManager.setLookAndFeel(new NimbusLookAndFeel());
		} catch (UnsupportedLookAndFeelException e1) {
			e1.printStackTrace();
		}
		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MybatisPlusGeneraterUI frame = new MybatisPlusGeneraterUI();
					frame.setLocationRelativeTo(null);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	/**
	 * @function 读取缓存文件并初始化表单
	 * @author 肖荣辉
	 * @date 2021年7月21日
	*/
	public void  readCacheAndInitForm() {
		
		File file = new File(CACHE_FILE_PATH);
		
		console("欢迎你使用代码生成器~");
		
		if(!file.exists()) 
			return;
		
		console("从缓存文件" +  CACHE_FILE_NAME + "读取上一次的配置信息...");
		
		try {
			String txt = FileUtils.readFileToString(CACHE_FILE_PATH);
			UserInput userInput = JSONObject.parseObject(txt, UserInput.class);
			setFormByUserInput(userInput);
		} catch (IOException e) {
			console("读取失败..");
			e.printStackTrace();
		}
		
	}
	
	/**
	 * @function 缓存用户输出
	 * @author 肖荣辉
	 * @throws IOException 
	 * @date 2021年7月21日
	*/
	public void cacheUserInput(UserInput userInput)  {
		
		try {
			
			String txt = JSON.toJSONString(userInput);
			
			FileUtils.saveTxtFile(CACHE_FILE_PATH, txt);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @function 根据userInput设置表单
	 * @author 肖荣辉
	 * @date 2021年7月21日
	*/
	public void setFormByUserInput(UserInput userInput) {
		
		DataBaseConfig config = userInput.getConfig();
		
		textField.setText(config.getIp());
		textField_2.setText(config.getPort());
		txtKm.setText(config.getDataBaseName());
		txtRoot.setText(config.getDbUserName());
		txtRoot_1.setText(config.getPassword());
		txtComewellkmservicecontrollers.setText(userInput.getControllerPackage());
		txtComewellkmserviceservice.setText(userInput.getServicePackage());
		txtComewellkmapientity.setText(userInput.getEntityPackage());
		txtComewellkmapidto.setText(userInput.getDtoPackage());
		txtComewellkmservicemapper.setText(userInput.getDaoPackage());
		txtComewellkmapirequestaddrequest.setText(userInput.getAddRequestPackage());
		txtComewellkmapirequestupdaterequest.setText(userInput.getUpdateRequestPackage());
		txtComewellkmapirequestqueryrequest.setText(userInput.getQueryRequestPackage());
		textField_15.setText(userInput.getOutputDirPath());
		authorInput.setText(userInput.getAuthorName());
	}
	
	
	/**
	 * @function 获取用户输入
	 * @author 肖荣辉
	 * @date 2021年7月21日
	*/
	public  UserInput getUserInput() {
		
		String ip = StringUtils.trim(textField.getText());
		String port = StringUtils.trim(textField_2.getText());
		String dataBaseName = StringUtils.trim(txtKm.getText());
		String dbUserName = StringUtils.trim(txtRoot.getText());
		String password = StringUtils.trim(txtRoot_1.getText());
		String author = StringUtils.trim(authorInput.getText());
		String controllerPackage = StringUtils.trim(txtComewellkmservicecontrollers.getText());
		String servicePackage = StringUtils.trim(txtComewellkmserviceservice.getText());
		String entityPackage = StringUtils.trim(txtComewellkmapientity.getText());
		String dtoPackage = StringUtils.trim(txtComewellkmapidto.getText());
		String mapperPackage = StringUtils.trim(txtComewellkmservicemapper.getText());
		String addRequestPackage = StringUtils.trim(txtComewellkmapirequestaddrequest.getText());
		String updateRequestPackage = StringUtils.trim(txtComewellkmapirequestupdaterequest.getText());
		String queryRequestPackage = StringUtils.trim(txtComewellkmapirequestqueryrequest.getText());
		String outDirPath =  StringUtils.trim(textField_15.getText());
		
		UserInput userInput = new UserInput();
		DataBaseConfig config = new DataBaseConfig(ip, port, dataBaseName, dbUserName, password);
		
		userInput.setConfig(config);
		
		userInput.setAddRequestPackage(addRequestPackage);
		userInput.setUpdateRequestPackage(updateRequestPackage);
		userInput.setQueryRequestPackage(queryRequestPackage);
		userInput.setControllerPackage(controllerPackage);
		userInput.setDtoPackage(dtoPackage);
		userInput.setEntityPackage(entityPackage);
		userInput.setDaoPackage(mapperPackage);
		userInput.setServicePackage(servicePackage);
		userInput.setAuthorName(StringUtils.isBlank(author) ? "SY-CodeGenerater" : author);
		userInput.setOutputDirPath(outDirPath);
		
		return userInput;
	}
	
	/**
	 * @function 控制台输出信息
	 * @author 肖荣辉
	 * @date 2021年7月21日
	*/
	public void console(String msg) {
		textArea.setText(textArea.getText() + "\n" + msg);
	}
	
	/**
	 * @function 启用生成按钮
	 * @author 肖荣辉
	 * @date 2021年7月21日
	*/
	public void enableGeneraterBtn() {
		btnNewButton.setEnabled(true);
	}
}
